<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="询价单标题" prop="title">
				<el-input v-model="form.title" placeholder="询价单标题" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="询价单号" prop="inquiryCode">
						<el-input v-model="form.inquiryCode" placeholder="询价单号" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结算币种" prop="cashType">
						<sc-select v-model="form.cashType" :params="{code:'cashType'}" :apiObj="$API.crm.crmDic.getCrmDicByCode" clearable filterable style="width: 100%" @change="getNoticeTemplateListByType"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="客户名称" prop="customerId">
						<el-select
							v-model="form.customerId"
							filterable
							remote
							reserve-keyword
							placeholder="请输入关键词"
							:remote-method="remoteMethod"
							:loading="loading"
							style="width:100%"
						>
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="支付方式" prop="payType">
						<sc-select v-model="form.payType" :params="{code:'payType'}" :apiObj="$API.crm.crmDic.getCrmDicByCode" clearable filterable style="width: 100%" @change="getNoticeTemplateListByType"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="联系人" prop="linkMan">
						<el-input v-model="form.linkMan" placeholder="联系人" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="联系电话" prop="tel">
						<el-input v-model="form.tel" placeholder="联系电话" clearable maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-divider content-position="center">询价明细</el-divider>
			<sc-form-table v-model="mx" :addTemplate="addTemplate" drag-sort placeholder="暂无数据">
				<el-table-column prop="productId" label="产品名称" width="200">
					<template #default="scope">
							<el-select
								v-model="scope.row.productId"
								filterable
								remote
								reserve-keyword
								placeholder="请输入关键词"
								:remote-method="remoteProductMethod"
								:loading="loading"
								style="width:100%"
								@change="setOtherValue(scope.row, scope.$index)"
							>
								<el-option v-for="item in productOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
							</el-select>
					</template>
				</el-table-column>
				<el-table-column prop="materialCode" label="产品编码" width="150" :show-overflow-tooltip="true">
					<template #default="scope">
						{{scope.row.materialCode}}
					</template>
				</el-table-column>
				<el-table-column prop="model" label="产品型号" width="100" align="center" :show-overflow-tooltip="true">
					<template #default="scope">
						{{scope.row.model}}
					</template>
				</el-table-column>
				<el-table-column prop="count" label="数量" width="100">
					<template #default="scope">
						<el-input-number v-model="scope.row.count" clearable controls-position="right" style="width: 80px;"></el-input-number>
					</template>
				</el-table-column>
				<el-table-column prop="unit" label="单位" width="100" align="center">
					<template #default="scope">
						<sc-select v-model="scope.row.unit" :params="{code:'crmUnit'}" :apiObj="$API.crm.crmDic.getCrmDicByCode" clearable filterable style="width: 100%"></sc-select>
					</template>
				</el-table-column>
				<el-table-column prop="delivery" label="交货期" width="150" align="center">
					<template #default="scope">
						<el-date-picker v-model="scope.row.delivery" placeholder="交货期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</template>
				</el-table-column>
				<el-table-column prop="price" label="单价" width="100" align="center" :show-overflow-tooltip="true">
					<template #default="scope">
						{{scope.row.price}}
					</template>
				</el-table-column>
				<el-table-column prop="guidePrice" label="指导价" width="100" align="center" :show-overflow-tooltip="true">
					<template #default="scope">
						{{scope.row.guidePrice}}
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="备注" align="center">
					<template #default="scope">
						<el-input v-model="scope.row.remark" clearable></el-input>
					</template>
				</el-table-column>
			</sc-form-table>
			<el-row :gutter="24" style="margin-top: 10px;">
				<el-col :lg="12">
					<el-form-item label="报价时间" prop="endTime">
						<el-date-picker v-model="form.endTime" placeholder="报价时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="送货地址" prop="sendAdd">
						<el-input v-model="form.sendAdd" placeholder="送货地址" clearable maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="客户性质" prop="customerNature">
						<el-radio-group v-model="form.customerNature">
							<el-radio-button label="1">终端用户</el-radio-button>
							<el-radio-button label="0">合作伙伴</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="包装要求" prop="packCharges">
						<el-radio-group v-model="form.packCharges">
							<el-radio-button label="1">需要包装</el-radio-button>
							<el-radio-button label="0">不需要包装</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="是否含税" prop="taxFlag">
						<el-radio-group v-model="form.taxFlag">
							<el-radio-button label="1">含税</el-radio-button>
							<el-radio-button label="0">不含税</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="是否开票" prop="invoiceFlag">
						<el-radio-group v-model="form.invoiceFlag">
							<el-radio-button label="1">开票</el-radio-button>
							<el-radio-button label="0">不开票</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="询价备注">
				<el-input v-model="form.remark" placeholder="询价备注" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'crm'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import AttachMenu from "@/views/module/attachMenu";

export default {
	emits: ['success', 'closed'],
	components: {
		AttachMenu
	},
	data() {
		return {
			addTemplate: {
				productId: '',
				productName:"",
				materialCode: '',
				model: '',
				count: 0,
				unit:"",
				price:0,
				guidePrice:0,
				delivery:"",
				remark: "",
			},
			visible: false,
			isSaveing: false,
			loading:false,
			options: [],
			productOptions: [],
			mode: "add",
			titleMap: {
				add: '询价录入对话框',
				edit: '询价录入对话框',
				show: '查看'
			},
			mx:[],
			fileList: [],
			apiObj: this.$API.file.attach.uploadFile,
			form: {
				inquiryId: '',
				title: '',
				inquiryCode: '',
				cashType: '',
				customerId: '',
				payType: '',
				linkMan: '',
				tel: '',
				formJson: "",
				endTime: '',
				sendAdd: '',
				customerNature: '1',
				packCharges: '1',
				taxFlag: '1',
				invoiceFlag:'1',
				remark:"",
				attachId:""
			},
			rules: {
				title: [
					{required: true, message: '询价标题不能为空', trigger: 'blur'}
				],
				customerInquiryCode: [
					{required: true, message: '询价单号不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		async getAllCustomerListForSelect(customerId) {
			let res = await this.$API.crm.crmCustomer.getAllCustomerListForSelect.get({customerId: customerId});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async remoteMethod(keyword) {
			let res = await this.$API.crm.crmCustomer.getAllCustomerListForSelect.get({keyword: keyword});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async remoteProductMethod(keyword) {
			let res = await this.$API.crm.crmProduct.getAllProductListForSelect.get({keyword: keyword});
			if (res.code == 200) {
				this.productOptions = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async getAllProductListForSelect(productId){
			let res = await this.$API.crm.crmProduct.getAllProductListForSelect.get({productId: productId});
			if (res.code == 200) {
				this.productOptions = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		setOtherValue(row,index)
		{
			this.productOptions.forEach(item=>{
				if(item.value==row.productId)
				{
					row.materialCode=item.materialCode;
					row.productName = item.label;
					row.model = item.model;
					row.price = item.price
					row.guidePrice = item.guidePrice
				}
			})

		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.form.formJson = JSON.stringify(this.mx);
					var res;
					if (this.mode == "add") {
						res = await this.$API.crm.crmInquiry.insertCrmInquiry.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.crm.crmInquiry.updateInquiry.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		async setData(data) {
			this.form.inquiryId = data.inquiryId
			this.form.title = data.title
			this.form.inquiryCode = data.inquiryCode
			this.form.cashType = data.cashType
			this.form.customerId = data.customerId
			this.form.payType = data.payType
			this.form.linkMan = data.linkMan
			this.form.tel = data.tel
			this.form.formJson = data.formJson
			this.form.endTime = data.endTime
			this.form.sendAdd = data.sendAdd
			this.form.customerNature = data.customerNature
			this.form.packCharges = data.packCharges
			this.form.taxFlag = data.taxFlag
			this.form.invoiceFlag = data.invoiceFlag
			this.form.remark = data.remark
			this.form.attachId = data.attachId
			if(data.formJson!=undefined&&data.formJson!='')
			{
				let listMx = JSON.parse(data.formJson);
				let params=[];
				listMx.forEach(item=>{
					params.push(item.productId);
				})
				let res = await this.$API.crm.crmProduct.getCrmProductListByIdsForSelect.get({ids:params});
				if (res.code == 200) {
					this.productOptions = res.data;
					this.mx = listMx;
				}
			}
			this.getAllCustomerListForSelect(data.customerId);
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
